package com.Data;


import java.util.Comparator;
import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import com.SoapClasses.SoapUser;
import com.shareup.R;
import com.utils.AlertDialogHelper;


public class User extends AsyncTask<Integer, String, User[]> implements Comparable<User> {

    public static final int ACTION_ADD = 0;

    public static final int ACTION_DELETE = 1;

    public static final int ACTION_DELETE_COVER = 6;

    public static final int ACTION_GET_INFO = 2;

    public static final int ACTION_GET_LOGIN = 3;

    public static final int ACTION_UPDATE = 4;

    public static final int ACTION_UPDATE_STATE = 5;

    public static Comparator<User> UserComparator = new Comparator<User>() {

        @Override
        public int compare(User original, User compare) {

            String nombre1 = original.getRealName();
            String nombre2 = compare.getRealName();
            return nombre1.compareTo(nombre2);
        }
    };

    private boolean ActiveLocation;

    private int Blur;

    private onAddUserCompleted callbackAddUser;

    private onDeleteCompleted callbackDelete;

    private onGetUserInfoCompleted callbackGetInfo;

    private onGetLoginCompleted callbackGetLogin;

    private onUpdateInfoCompleted callbackUpdateInfo;

    private onUpdateStateCompleted callbackUpdateState;

    private Context context;

    private String CoverPicture;

    private String Description;

    private String Email;

    private int ID;

    private int IDProfile;

    private int isFollowingMe;

    private String Password;

    private int Privacy;

    private String ProfilePicture;

    private AlertDialog progress;

    private String RealName;

    private int Sex;

    private int State;

    private String Username;

    public interface onAddUserCompleted {

        void Completed(String result);
    }

    public interface onDeleteCompleted {

        void Completed(String result);
    }

    public interface onGetLoginCompleted {

        void Completed(User result);
    }

    public interface onGetUserInfoCompleted {

        void Completed(User result);
    }

    public interface onUpdateInfoCompleted {

        void Completed(String result);
    }

    public interface onUpdateStateCompleted {

        void Completed(String result);
    }

    @Override
    public int compareTo(User compareUser) {

        // TODO Auto-generated method stub
        return 0;
    }

    public int getBlur() {

        return Blur;
    }

    public onAddUserCompleted getCallbackAddUser() {

        return callbackAddUser;
    }

    public onGetUserInfoCompleted getCallbackGetInfo() {

        return callbackGetInfo;
    }

    public onGetLoginCompleted getCallbackGetLogin() {

        return callbackGetLogin;
    }

    public onUpdateInfoCompleted getCallbackUpdateInfo() {

        return callbackUpdateInfo;
    }

    public onUpdateStateCompleted getCallbackUpdateState() {

        return callbackUpdateState;
    }

    public String getCoverPicture() {

        return CoverPicture;
    }

    public String getDescription() {

        return Description;
    }

    public String getEmail() {

        return Email;
    }

    public int getID() {

        return ID;
    }

    public int getIDProfile() {

        return IDProfile;
    }

    public int getIsFollowingMe() {

        return isFollowingMe;
    }

    public String getPassword() {

        return Password;
    }

    public int getPrivacy() {

        return this.Privacy;
    }

    public String getProfilePicture() {

        return ProfilePicture;
    }

    public String getRealName() {

        return RealName;
    }

    public int getSex() {

        return Sex;
    }

    public int getState() {

        return State;
    }

    public String getUsername() {

        return Username;
    }

    public boolean isActiveLocation() {

        return ActiveLocation;
    }

    public void setActiveLocation(boolean activeLocation) {

        ActiveLocation = activeLocation;
    }

    public void setBlur(int blur) {

        this.Blur = blur;
    }

    public void setCallbackAddUser(onAddUserCompleted callbackAddUser) {

        this.callbackAddUser = callbackAddUser;
    }

    public void setCallbackDelete(onDeleteCompleted callbackDelete) {

        this.callbackDelete = callbackDelete;
    }

    public void setCallbackGetInfo(onGetUserInfoCompleted callbackGetInfo) {

        this.callbackGetInfo = callbackGetInfo;
    }

    public void setCallbackGetLogin(onGetLoginCompleted callbackGetLogin) {

        this.callbackGetLogin = callbackGetLogin;
    }

    public void setCallbackUpdateInfo(onUpdateInfoCompleted callbackUpdateInfo) {

        this.callbackUpdateInfo = callbackUpdateInfo;
    }

    public void setCallbackUpdateState(onUpdateStateCompleted callbackUpdateState) {

        this.callbackUpdateState = callbackUpdateState;
    }

    public void setContext(Context context) {

        this.context = context;
    }

    public void setCoverPicture(String coverPicture) {

        CoverPicture = coverPicture;
    }

    public void setDescription(String description) {

        Description = description;
    }

    public void setEmail(String email) {

        Email = email;
    }

    public void setID(int iD) {

        ID = iD;
    }

    public void setIDProfile(int iDProfile) {

        IDProfile = iDProfile;
    }

    public void setIsFollowingMe(int isFollowingMe) {

        this.isFollowingMe = isFollowingMe;
    }

    public void setPassword(String password) {

        Password = password;
    }

    public void setPrivacy(int privacy) {

        this.Privacy = privacy;
    }

    public void setProfilePicture(String profilePicture) {

        ProfilePicture = profilePicture;
    }

    public void setRealName(String realName) {

        RealName = realName;
    }

    public void setSex(int sex) {

        Sex = sex;
    }

    public void setState(int state) {

        State = state;
    }

    public void setUsername(String username) {

        Username = username;
    }

    // ////////////////////////////////////////////////////////////////////
    @Override
    protected User[] doInBackground(Integer... action) {

        User[] uss;
        String[] data;
        switch (action[0]) {
            case ACTION_ADD:
                data = new String[5];
                data[0] = this.getUsername();
                data[1] = this.getPassword();
                data[2] = this.getRealName();
                data[3] = this.getEmail();
                data[4] = String.valueOf(this.getSex());
                //
                String res = new SoapUser().addUser(data);
                //
                uss = new User[1];
                uss[0] = new User();
                uss[0].setDescription(res);
                return uss;

            case ACTION_DELETE:
                return null;
            case ACTION_GET_INFO:
                uss = new User[1];
                uss[0] = new SoapUser().getInfo(this.ID);
                return uss;
            case ACTION_GET_LOGIN:
                User[] us = new User[1];
                us[0] = new SoapUser().login(this.Username, this.Password);
                return us;
            case ACTION_UPDATE:
                SoapUser sp = new SoapUser();
                data = new String[4];
                data[0] = this.RealName;
                data[1] = this.Description;
                data[2] = this.Email;
                data[3] = String.valueOf(this.ID);
                uss = new User[1];
                uss[0] = new User();
                uss[0].setDescription(sp.updateProfile(data));
                return uss;
            case ACTION_UPDATE_STATE:
                uss = new User[1];
                uss[0] = new User();
                uss[0].setDescription(new SoapUser().updateState(this.State));
                return uss;
            case ACTION_DELETE_COVER:
                uss = new User[1];
                uss[0] = new User();
                uss[0].setDescription(new SoapUser().deletePictureCover(this.ID, this.CoverPicture));
                return uss;
            default:
                return null;
        }

    }

    @Override
    protected void onPostExecute(User[] result) {

        if (callbackGetLogin != null) {
            callbackGetLogin.Completed(result[0]);
            progress.dismiss();
        }

        if (callbackAddUser != null) {
            progress.dismiss();
            callbackAddUser.Completed(result[0].getDescription());
        }

        if (callbackGetInfo != null)
            callbackGetInfo.Completed(result[0]);
        if (callbackUpdateInfo != null) {
            progress.dismiss();
            callbackUpdateInfo.Completed(result[0].getDescription());
        }

        if (callbackUpdateState != null)
            callbackUpdateState.Completed(result[0].getDescription());

        if (callbackDelete != null)
            callbackDelete.Completed(result[0].getDescription());

    }

    @Override
    protected void onPreExecute() {

        if (callbackUpdateInfo != null) {
            progress = AlertDialogHelper.setProgressDialog(progress, context.getResources().getString(R.string.dialog_saving), context);
            progress.show();
        }

        if (callbackAddUser != null) {
            progress = AlertDialogHelper.setProgressDialog(progress, context.getResources().getString(R.string.dialog_adding_new_user), context);
            progress.show();
        }
        if (callbackGetLogin != null) {
            progress = AlertDialogHelper.setProgressDialog(progress, context.getResources().getString(R.string.dialog_login_wait), context);
            progress.show();
        }

    }
}
